<template>
	<view class="main-box_G">
		<!-- 时间选择盒子 -->
		<!-- <u-sticky> -->
		<view class="timeScaleBox">
			<view class="timeBox" @click="selectDate">
				<image src="/static/labBusiness-icons/clock.svg"
					class="clockIcon"></image>
				<text>{{dateValue ? formatTime(dateValue) : '- 年 - 月 - 日'}}</text>
				<u-icon name="arrow-down" color="#364254" size="18"></u-icon>
			</view>
			<view class="timeBox hourBox">
				<view><text
						@click="selectStartTime">{{startTimeValue}}</text><text
						class="hourSplitLine">-</text><text
						@click="selectEndTime">{{endTimeValue}}</text></view>
				<u-icon name="arrow-down" color="#364254" size="18"
					@click="selectEndTime"></u-icon>
			</view>
			<!-- <button shape="square" class="searchBtn">查找</button> -->

			<u-button class="u-button" color="#0A7AFF" text="查找"
				@click="searchBtnClick"></u-button>
		</view>
		<!-- </u-sticky> -->

		<!-- 时间选择器 -->
		<u-datetime-picker :show="showDatePicker" v-model="dateValue"
			mode="date" @cancel="datePickerCancel" @confirm="datePickerConfirm">
		</u-datetime-picker>
		<u-datetime-picker title="开始时间" :show="showStartTimePicker"
			v-model="startTimeValue" :maxHour="maxEndHour"
			:maxMinute="maxEndMinute" mode="time"
			@cancel="startTimePickerCancel" @confirm="startTimePickerConfirm">
		</u-datetime-picker>
		<u-datetime-picker title="结束时间" :show="showEndTimePicker"
			v-model="endTimeValue" :minHour="minStartHour"
			:minMinute="minStartMinute" mode="time"
			@cancel="endTimePickerCancel" @confirm="endTimePickerConfirm">
		</u-datetime-picker>
		<view class="data-list_G">
			<DeviceCell v-for="item in labData" :key="item.id"
				:title="item.title" :image="item.img" imgRadius="4" btnText="预约"
				btnColor="#FFA245" @click="gotoDetail(item.id)">
				<view class="data-info_G">
					<view class="labLocation">
						<!-- 		<u--image width="36rpx" height="36rpx"
							src="../../../../static/labBusiness-icons/location.svg"
							class="img">
						</u--image> -->
						<text> {{ item.college }}</text>
					</view>
				</view>
			</DeviceCell>
		</view>
		<u-loadmore v-if="labData.length" :status="loadmoreStatus"
			@loadmore="requestListData" />
	</view>
</template>

<script>
	import {
		getLabList
	} from '@/service/lab.js'

	export default {
		name: 'LabBooking',
		data() {
			return {
				showDatePicker: false,
				showStartTimePicker: false,
				showEndTimePicker: false,
				dateValue: Number(new Date()),
				startTimeValue: '00:00',
				endTimeValue: '00:00',
				minStartHour: 0,
				minStartMinute: 0,
				maxEndHour: 23,
				maxEndMinute: 59,
				listParams: {
					offset: 1,
					limit: 10,
					fromCreateTime: '',
					toCreateTime: ''
				},
				labData: [],
				currentPage: 1,
				totalPage: 1,
				loadmoreStatus: 'loadmore'
			}
		},
		onLoad() {
			this.getLabList()
		},
		async onPullDownRefresh() {
			this.listParams.offset = 1
			await this.getLabList('pullDown')
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		onReachBottom() {
			this.requestListData()
		},
		watch: {
			dateValue(newValue) {
				console.log(newValue)
			}
		},
		computed: {

		},
		methods: {
			selectDate() {
				this.showDatePicker = true
			},
			datePickerCancel() {
				this.showDatePicker = false
			},
			datePickerConfirm() {
				// this.selectTimeValue = this.formatTime(this.dateValue)
				this.showDatePicker = false
			},
			formatTime(timeValue, type = 0) {
				// type0 格式化带有年月日文字的日期 type1 格式化'-'隔开的日期
				const date = new Date(timeValue)
				if (type === 0) {
					return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
				} else if (type === 1) {
					return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
				}
			},
			selectStartTime() {
				this.showStartTimePicker = true
			},
			startTimePickerCancel() {
				this.showStartTimePicker = false
			},
			startTimePickerConfirm(time) {
				const timeVal = time.value.split(':')
				this.minStartHour = timeVal[0] - 0
				this.minStartMinute = timeVal[1] - 0
				this.showStartTimePicker = false
			},
			selectEndTime() {
				this.showEndTimePicker = true
			},
			endTimePickerCancel() {
				this.showEndTimePicker = false
			},
			endTimePickerConfirm(time) {
				const timeVal = time.value.split(':')
				const startTime = timeVal[0] - 0
				const endTime = timeVal[1] - 0
				if (startTime !== 0 || endTime !== 0) {
					this.maxEndHour = startTime
					this.maxEndMinute = endTime
				}

				this.showEndTimePicker = false
			},
			// 获取实验室列表
			async getLabList(type) {
				const {
					code,
					data,
					msg
				} = await getLabList(this.listParams)
				console.log({
					code,
					data,
					msg
				})
				if (code === 0 && data) {
					if (data.list.length) {
						if (type === 'pullDown') {
							this.labData = []
						}
						this.labData.push(...data.list.map(item => {
							return {
								id: item.id,
								title: item.name,
								img: item.img ? item.img[0] : '',
								college: item.college.name
							}
						}))
					} else {
						this.labData = data.list
					}
					this.currentPage = data.currPage
					this.totalPage = data.totalPage
				}
			},
			// 触底请求列表数据
			async requestListData() {
				// 如果列表数据为最后一页
				if (this.currentPage >= this.totalPage) {
					this.loadmoreStatus = 'nomore'
					return
				} else {
					this.loadmoreStatus = 'loading'
					this.listParams.offset++
					await this.getLabList()
					setTimeout(() => {
						if (this.currentPage >= this.totalPage) {
							this.loadmoreStatus = 'nomore'
							return
						}
						this.loadmoreStatus = 'loadmore'
					}, 300)
				}
			},
			// 搜索按钮点击事件
			searchBtnClick() {
				console.log(this.formatTime(this.dateValue, 1))
				console.log(this.startTimeValue)
				console.log(this.endTimeValue)
				const formatTimeValue = this.formatTime(this.dateValue, 1)
				this.listParams.fromCreateTime =
					`${formatTimeValue} ${this.startTimeValue}`
				this.listParams.toCreateTime =
					`${formatTimeValue} ${this.endTimeValue}`
				console.log(this.listParams)
				this.getLabList()
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: `/subpkg/labBusiness/lab-booking-form/lab-booking-form?id=${id}`,
				})
			}
		},

	}
</script>

<style lang="scss">
	.main-box_G {
		// background-color: #F6F6F6;
		padding: 84rpx 18rpx 0 18rpx;
	}

	.timeScaleBox {
		position: fixed;
		display: flex;
		top: 2rpx;
		left: 0;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		height: 84rpx;
		line-height: 84rpx;
		padding: 0 22rpx 0 32rpx;
		background-color: #fff;


		.timeBox {
			display: flex;
			font-size: 28rpx;
			align-items: center;

			.clockIcon {
				width: 48rpx;
				height: 48rpx;
				margin-right: 24rpx;
			}


		}

		.hourBox {
			margin-left: 32rpx;

			.hourSplitLine {
				color: #cdcdcd;
				margin: 0 5rpx;
			}
		}

	}

	.labLocation {
		display: flex;
		align-items: center;

		.u-image {
			margin-bottom: 10rpx;
		}

		text {
			font-size: 24rpx;
		}
	}

	// .searchBtn {
	// 	width: 130rpx;
	// 	height: 52rpx;
	// 	line-height: 52rpx;
	// 	font-size: 28rpx;
	// 	border-radius: 8rpx;
	// 	background-color: #0A7AFF;
	// 	color: #fff;
	// 	margin-top: 16rpx;
	// 	margin-left: 30rpx;
	// 	margin-right: 0;
	// }
</style>
